//
//  XWalletContentTableCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/22.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XWalletContentTableCell.h"

@interface XWalletContentTableCell()

@end
@implementation XWalletContentTableCell

- (void)setData:(NSArray<TokenInfo *> *)data{
    _data = data;
    [self.table reloadData];
}
- (void)setEosData:(NSArray<TokenInfo *> *)eosData{
    _eosData = eosData;
    [self.table reloadData];
}

- (void)setupUI{
    UIView*backView = [UIView new];
    backView.backgroundColor = UIColor.whiteColor;
    backView.layer.cornerRadius = AdaptedWidth(7.0f);
    [backView.layer setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    [self.contentView addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(kNormalSpace);
        make.right.bottom.mas_equalTo(-kNormalSpace);
    }];
    [backView addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-AdaptedWidth(10.f));
    }];
    
    UIView*headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0,0, XWalletContentTableHeadHeight)];
    UILabel *tipLab = [UILabel new];
    tipLab.font = kBoldFont(16);
    tipLab.text = XLocalized(@"资产");
    [headV addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.bottom.mas_equalTo(-AdaptedWidth(3));
    }];
    
    UIButton *btn = [UIButton new];
    [btn setImage:IMAGE_NAMED(@"black_add") forState:UIControlStateNormal];
    [headV addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tipLab.mas_centerY);
        make.right.mas_equalTo(AdaptedWidth(-7));
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    kWeakSelf(self);
    [btn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.AddBlock?:weakself.AddBlock();
    }];
    
    self.table.tableHeaderView = headV;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return self.eosData.count;
    }
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XWalletContentSubTableCell *cell = [XWalletContentSubTableCell cellForTableView:tableView];
   
    TokenInfo*token;
    if (indexPath.section == 0) {
        cell.line.hidden = indexPath.row == 0;
        token = [self.eosData objectOrNilAtIndex:indexPath.row];
    }else{
//        cell.line.hidden = YES;
        token = [self.data objectOrNilAtIndex:indexPath.row];
    }
    [cell.iconV sd_setImageWithURL:[NSURL URLWithString: token.logo] placeholderImage:IMAGE_NAMED(@"default_person")];
    cell.nameLab.text = token.symbol;
    if (IS_Property_Hidden) {
        cell.numLab.text = @"****";
        cell.valueLab.text = @"****";
    }else{
        cell.numLab.text = [token.balance getDigital].length?[token.balance getDigital]:@"0.0000";
        cell.valueLab.text = [NSString stringWithFormat:@"≈ %@ %@",@"¥",IsNilOrNull(token.valuation)?@"0":handlerDecimalNumber(token.valuation,NSRoundPlain, kSymbolLength_EOS).stringValue];
    }
    
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ///删除的是自己所有的资产  需要记录 下次不再显示
        !self.DeleteBlock?:self.DeleteBlock(indexPath,[self.eosData objectOrNilAtIndex:indexPath.row]);
    }else{
        ///删除的是s自己手动r添加的资产
        !self.DeleteBlock?:self.DeleteBlock(indexPath,[self.data objectOrNilAtIndex:indexPath.row]);
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TokenInfo*token =  [self.eosData objectOrNilAtIndex:indexPath.row];
        if ([token.code isEqualToString:kContractName_EOSIOTOKEN] || [token.code isEqualToString:kContractName_BEESIOTOKEN]) {
            return NO;
        }
    }
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        !self.DidSeletBlock?:self.DidSeletBlock(indexPath,[self.eosData objectOrNilAtIndex:indexPath.row]);
    }else{
        !self.DidSeletBlock?:self.DidSeletBlock(indexPath,[self.data objectOrNilAtIndex:indexPath.row]);
    }
}
#pragma mark - getter
- (UITableView*)table{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.rowHeight = XWalletContentTableCellHeight;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.scrollEnabled = NO;
        _table.backgroundColor = UIColor.clearColor;
    }
    return _table;
}
@end


@implementation XWalletContentSubTableCell

- (void)setupUI{
    self.iconV = [UIImageView new];
    [self.contentView addSubview:self.iconV];
    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(36), AdaptedWidth(36)));
    }];
    ViewBorderRadius(self.iconV, AdaptedWidth(18), 1, kLineColor);
    self.nameLab = [UILabel new];
    self.nameLab.font = kBoldFont(16);
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconV.mas_right).offset(kNormalSpace);
        make.centerY.mas_equalTo(0);
    }];
    
    self.numLab = [UILabel new];
    self.numLab.font = kBoldFont(16);
    [self.contentView addSubview:self.numLab];
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(self.iconV.mas_top);
    }];
    
    self.valueLab = [UILabel new];
    self.valueLab.font = kDefaultFont(12);
    self.valueLab.textColor = UIColorHex(#B5B5B5);
    [self.contentView addSubview:self.valueLab];
    [self.valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numLab.mas_right);
        make.bottom.equalTo(self.iconV.mas_bottom);
    }];
    
    self.line = [UIView new];
    self.line.backgroundColor = kLineColor;
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(CGFloatFromPixel(1));
    }];
}

@end
